package top.houry.im.client.event;

import io.netty.channel.ChannelHandlerContext;
import top.houry.im.client.proto.ImMessage;
import top.houry.im.client.utils.ImClientChannelUtils;
import top.houry.im.ui.interfaces.ImUiMainEvent;

public class ImClientUiMainEvent implements ImUiMainEvent {
    @Override
    public void sendMsg(String fromUserId, String toUserId, String talkId, int talkType, String msg) {
        ChannelHandlerContext ctx = ImClientChannelUtils.getChannelHandlerContext();

        ImMessage.ChatReqMessage.Builder chatReqMsg = ImMessage.ChatReqMessage.newBuilder();
        chatReqMsg.setChatMsg(msg);
        chatReqMsg.setFromUserId(fromUserId);
        chatReqMsg.setToUserId(toUserId);


        chatReqMsg.setUserName("后退杨");

        ImMessage.Client.Builder client = ImMessage.Client.newBuilder();
        client.setMsgType("client.chat.msg.forward");
        client.setBytesData(chatReqMsg.build().toByteString());


        ctx.writeAndFlush(client).addListener(future -> {
            if (!future.isSuccess()) {
                future.cause().printStackTrace();
            }
        });


    }

    @Override
    public void closeMainUi() {
        ChannelHandlerContext ctx = ImClientChannelUtils.getChannelHandlerContext();
        ctx.close();
    }
}
